var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');



// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();
var jsonWrite = function(res, ret) {
  if(typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    });
  } else {
    res.json(ret);
  }
};



//查看订单对应的地址
router.post('/selectById', (req, res) => {
	//调用sql语句 --- sqlMap.js文件夹
  let sql = $sql.dizhi.selectOne;
  //接收前台数据
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.did], function(err, result) {
    if (err) {
      console.log("操作失败"+err);
	  jsonWrite(res, result);
    }
    if (result) {
      jsonWrite(res, result);
    }
  });
});

// 查看用户的所以地址
router.post('/selecUid', (req, res) => {
	//调用sql语句 --- sqlMap.js文件夹
  let sql = $sql.dizhi.selecUid;
  //接收前台数据
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.uid], function(err, result) {
    if (err) {
      console.log("操作失败"+err);
	  jsonWrite(res, result);
    }
    if (result) {
      jsonWrite(res, result);
    }
  });
});



// 添加用户的所以地址
router.post('/add', (req, res) => {
	//调用sql语句 --- sqlMap.js文件夹
  let sql = $sql.dizhi.add;
  //接收前台数据
  let p = req.body;
  console.log(p);
  conn.query(sql, [p.uid,p.uname,p.utel,p.sheng,p.city,p.qu,p.address], function(err, result) {
    if (err) {
      console.log("操作失败"+err);
	  jsonWrite(res, result);
    }
    if (result) {
      jsonWrite(res, result);
    }
  });
});



module.exports = router;